{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CHEMICAL SIMULATION WITH VQE\n",
    "\n",
    "This notebook shows how to use VQE in Qiskit's Aqua to obtain the dissociation profile of a molecule. \n",
    "\n",
    "We start by defining the molecule ($H_2$ in this case) and the method we will use to transform the fermionic hamiltonian into a qubit hamiltonian."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit.chemistry.drivers import PySCFDriver, UnitsType, Molecule\n",
    "from qiskit.chemistry.transformations import FermionicTransformation, FermionicQubitMappingType\n",
    "\n",
    "molecule = Molecule(geometry=[['H', [0., 0., 0.]],\n",
    "                              ['H', [0., 0., 0.735]]],\n",
    "                     charge=0, multiplicity=1)\n",
    "driver = PySCFDriver(molecule = molecule, unit=UnitsType.ANGSTROM, basis='sto3g')\n",
    "transformation = FermionicTransformation(qubit_mapping=FermionicQubitMappingType.JORDAN_WIGNER)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, we create a VQE instance that will use the UCCSD ansatz to estimate the ground state."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import Aer\n",
    "from qiskit.aqua import QuantumInstance\n",
    "from qiskit.chemistry.algorithms.ground_state_solvers.minimum_eigensolver_factories import VQEUCCSDFactory\n",
    "\n",
    "vqe_solver = VQEUCCSDFactory(QuantumInstance(Aer.get_backend('statevector_simulator')))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We run it and display the information obtained about the electronic structure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from qiskit.chemistry.algorithms.ground_state_solvers import GroundStateEigensolver\n",
    "\n",
    "calc = GroundStateEigensolver(transformation, vqe_solver)\n",
    "res = calc.solve(driver)\n",
    "\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For comparison, we can do the same with an exact eigensolver. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit.aqua.algorithms import NumPyMinimumEigensolver\n",
    "\n",
    "numpy_solver = NumPyMinimumEigensolver()\n",
    "\n",
    "calc = GroundStateEigensolver(transformation, numpy_solver)\n",
    "res = calc.solve(driver)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we use both the VQE and the exact solver to compute the molecule energy as a function of the distance between the atoms."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "distances = np.linspace(0.25, 3.0, 30)\n",
    "exact_energies = []\n",
    "vqe_energies = []\n",
    "for dist in distances:\n",
    "    molecule = Molecule(geometry=[['H', [0., 0., 0.]],\n",
    "                              ['H', [0., 0., dist]]],\n",
    "                     charge=0, multiplicity=1)\n",
    "    driver = PySCFDriver(molecule = molecule, unit=UnitsType.ANGSTROM, basis='sto3g')\n",
    "    # Exact solver\n",
    "    calc = GroundStateEigensolver(transformation, numpy_solver)\n",
    "    res = calc.solve(driver)\n",
    "    exact_energies.append(res.total_energies)\n",
    "    # VQE\n",
    "    calc = GroundStateEigensolver(transformation, vqe_solver)\n",
    "    res = calc.solve(driver)\n",
    "    vqe_energies.append(res.total_energies)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We plot the results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.plot(exact_energies, label = 'Exact solver')\n",
    "plt.plot(vqe_energies, label = 'VQE')\n",
    "plt.title('Dissociation profile')\n",
    "plt.xlabel('Interatomic distance')\n",
    "plt.legend()\n",
    "plt.ylabel('Energy');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
